<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 添加分类对话框 -->
      <el-dialog title="添加分类" :visible.sync="addCatDialog" width="50%" @close="resetForm()">
        <el-form :rules="addCatRules" ref="addCatRef" :model="addCat" label-width="120px">
          <el-form-item prop="cat_name" label="分类名称：">
            <el-input v-model="addCat.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="分类上级：">
            <el-cascader
              expand-trigger="hover"
              :options="catTwoList"
              v-model="catTwoSelected"
              :props="catTwoProps"
              @change="catTwoChange()"
              clearable
              style="width:100%"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCatDialog = false">取 消</el-button>
          <el-button type="primary" @click="tianjia()">确 定</el-button>
        </span>
      </el-dialog>
      <el-button type="primary" @click="showAddCatDialog()">添加分类</el-button>

      <el-table :data="catList" style="width: 100%" row-key="cat_id" stripe lazy>
        <el-table-column type="index" label="序号" width="80">
          <!-- <template slot-scope="inf" v-if="inf.row.cat_level === 0">
            <span>{{inf.$index + 1}}</span>
          </template>-->
        </el-table-column>
        <el-table-column prop="cat_name" label="分类名称" width="200">
          <template slot-scope="scope">
            <!-- <i class="zk-table--tree-icon zk-icon zk-icon-plus-square-o"></i> -->
            <span>{{ scope.row.cat_name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="true" label="是否有效">
          <i class="el-icon-circle-check" style="color:rgb(68, 178, 170);"></i>
        </el-table-column>
        <el-table-column prop="cat_level" label="等级" width="240">
          <template slot-scope="info">
            <el-tag v-if="info.row.cat_level==0">一级</el-tag>
            <el-tag v-else-if="info.row.cat_level==1" type="success">二级</el-tag>
            <el-tag v-else type="warning">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="340">
          <template>
            <el-button type="primary" icon="el-icon-edit" size="mini">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querycdt.pagenum"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="querycdt.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tot"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  created () {
    this.getCatList()
  },
  methods: {
    async getCatList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.querycdt
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      console.log(res.data)
      this.tot = res.data.total
      this.catList = res.data.result
    },
    /**  数据分页相关1 */
    // 每条记录条数变化的回调处理
    handleSizeChange (arg) {
      // arg: 变化后的记录条数
      // console.log(arg)
      this.querycdt.pagesize = arg
      // 重新根据条件获得数据
      this.getCatList()
    },
    // 当前页码变化的回调处理
    handleCurrentChange (arg) {
      // arg: 变化后的当前页码值
      // console.log(arg)
      this.querycdt.pagenum = arg
      // 根据变化后的页码重新获得数据
      this.getCatList()
    },
    async showAddCatDialog () {
      // 获取可供选取的二级分类信息
      const { data: dt } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      // 把数据传递给组件实例
      this.catTwoList = dt.data
      // 展示添加分类对话框
      this.addCatDialog = true
    },
    catTwoChange () {
      console.log(this.catTwoSelected)

      var len = this.catTwoSelected.length
      if (len === 0) {
        this.addCat.cat_pid = 0
        this.addCat.cat_level = 0
      } else {
        this.addCat.cat_pid = this.catTwoSelected[len - 1]
        this.addCat.cat_level = len
      }
    },
    tianjia () {
      this.$refs.addCatRef.validate(async valid => {
        if (valid === true) {
          const { data: dt } = await this.$http.post('categories', this.addCat)
          this.$message.success(dt.meta.msg)
          this.addCatDialog = false
          this.getCatList()
        }
      })
    },
    resetForm () {
      this.$refs.addCatRef.resetFields()
      this.catTwoSelected = []
      this.addCat.cat_pid = 0
      this.addCat.cat_level = 0
    }
  },
  data () {
    return {
      tot: 0,
      catList: [],
      querycdt: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 级联选择器数据属性部分
      catTwoProps: {
        value: 'cat_id', // 选择器有效数据
        label: 'cat_name', // 选择器显示的信息
        children: 'children' // 父子级衔接字段
      },
      catTwoSelected: [], // 接收 选中 的项目值
      catTwoList: [],
      addCatDialog: false,
      addCatRules: {
        cat_name: [{ required: true, message: '分类名称必填', trigger: 'blur' }]
      },
      addCat: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
